package hu.androidportal;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;

import com.google.android.apps.analytics.GoogleAnalyticsTracker;

public class TrackedActivity extends Activity implements Codes
{
	public final static String ACTION_BROWSE = "Browse";
	public final static String ACTION_SEND = "Send";
	public final static String ACTION_CLICK = "Click";

	private GoogleAnalyticsTracker tracker;
	protected String pageId;

	private boolean overrideTracker = false;

	@Override
	protected void onCreate( final Bundle savedInstanceState )
	{
		super.onCreate(savedInstanceState);

		if ( ANALYTICS_ACTIVE )
		{

			tracker = GoogleAnalyticsTracker.getInstance();
			tracker.start(TRACKER_ID, this);

			pageId = getClass().getName();
		}
	}

	@Override
	protected void onResume()
	{
		super.onResume();

		if ( ANALYTICS_ACTIVE && !overrideTracker )
		{
			tracker.trackPageView("/" + pageId);
		}
	}

	protected void trackPage( final String page )
	{
		if ( ANALYTICS_ACTIVE )
		{
			tracker.trackPageView(page);
		}
	}

	protected void trackClickEvent( final String action, final String data )
	{
		if ( ANALYTICS_ACTIVE )
		{
			tracker.trackEvent(pageId, action, data, 1);
		}
	}

	@Override
	protected void onDestroy()
	{
		super.onDestroy();

		if ( ANALYTICS_ACTIVE )
		{
			tracker.stop();
		}
	}

	public static void dispatch( final Context baseContext )
	{
		if ( ANALYTICS_ACTIVE )
		{
			final GoogleAnalyticsTracker tracker = GoogleAnalyticsTracker.getInstance();
			tracker.start(TRACKER_ID, baseContext);
			tracker.dispatch();
			tracker.stop();
		}
	}

	public void setOverrideTracker( final boolean overrideTracker )
	{
		this.overrideTracker = overrideTracker;
	}

	public boolean isOverrideTracker()
	{
		return overrideTracker;
	}
}
